package com.mjk.manager.centerconf.work;

import java.util.ArrayList;
import java.util.List;

import com.mjk.common.base.error.BeeError;
import com.mjk.common.base.response.BeeResponse;
import com.mjk.common.base.work.BaseWork;
import com.mjk.common.base.work.annotation.BeeWork;
import com.mjk.common.core.redis.datatype.RedisHashUtils;
import com.mjk.common.core.redis.datatype.RedisUtils;
import com.mjk.manager.centerconf.conf.CenterConf;
import com.mjk.manager.centerconf.dto.ConfDto;
import com.mjk.manager.centerconf.dto.RefreshEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;


/**
 * 分布式 配置中心 接口部分
 * @author jafek
 *
 */
@BeeWork(work="centerconf")
public class CenterConfWork extends BaseWork {
	
	@SuppressWarnings("rawtypes")
	@Autowired
	private RedisTemplate redisTemplate;
	
	/**
	 * 根据分类获取配置信息
     */
	@SuppressWarnings("unchecked")
	public BeeResponse getConfs(ConfDto dto){
		List<String> keys = RedisHashUtils.getKeys(redisTemplate, CenterConf.CONFIGURATION_CONTENT_KEY);
		List<ConfDto> list = new ArrayList<>();
		for(String key : keys){
			ConfDto tmp = new ConfDto();
			Object obj = RedisHashUtils.get(redisTemplate, CenterConf.CONFIGURATION_CONTENT_KEY, key);
			tmp.setName(key);
			tmp.setValue(obj.toString());	
			list.add(tmp);			
		}
		return BeeError.BEE_SUC.tranceError(list);
	}
	/**
	 * 修改配置
     */
	public BeeResponse updateConfValue(ConfDto dto) {
		//设置值
		RedisHashUtils.set(redisTemplate, CenterConf.CONFIGURATION_CONTENT_KEY, dto.getName(), dto.getValue());
		//通知
		RedisUtils.set(redisTemplate, CenterConf.CONFIGURATION_KEY, new RefreshEntity(dto.getDev(),dto.getIp()));
		return BeeError.BEE_SUC.tranceError("");
	}
}